package hust.ioic.oa.view.listener;

import hust.ioic.oa.domain.Department;
import hust.ioic.oa.domain.Function;
import hust.ioic.oa.service.DepartmentService;
import hust.ioic.oa.service.FunctionService;

import java.util.Collection;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class InitListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// 获取容器与相关的Service对象
		ApplicationContext ac = WebApplicationContextUtils
				.getWebApplicationContext(sce.getServletContext());
		FunctionService functionService = (FunctionService) ac
				.getBean("functionServiceImpl");

		// 准备数据：topPrivilegeList
		List<Function> topFunctionList = functionService.findTopFunction();
		sce.getServletContext()
				.setAttribute("topFunctionList", topFunctionList);

		// 准备数据：allPrivilegeUrls
		Collection<String> allFunctionUrls = functionService
				.getAllFunctionUrls();
		sce.getServletContext()
				.setAttribute("allFunctionUrls", allFunctionUrls);
	}

}
